﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using SimpleRest.Core;
using System.IO;
using SimpleRest.Core.Routing;
using SimpleRest.Core.Routing;
using SimpleRest.Core.Extensions.Handlers;
using System.Web.Configuration;

namespace SimpleRest.Core.Extensions
{
    /// <summary>
    /// When extended, this class provides the developer with an easy way to drive service registration and configuration with Initialize().
    /// </summary>
    public abstract class HttpApplicationBase : System.Web.HttpApplication, IRestBootstrapper
    {
        internal const string IgnoreRouteHandlerValue = "6282c9ac238844eeb80c10e960b86944=true";

        //public HttpApplicationBase()
        //    : this(true)
        //{
        //}
        //public HttpApplicationBase(bool isNativeRouteRegistrationEnabled)
        //{
        //    RouteUtility.IsNativeRouteRegistrationEnabled = isNativeRouteRegistrationEnabled;
        //}

        private bool? IsWebConfigHandlerRegistered { get; set; }

        /// <summary>
        /// Fired one time before Initialize()
        /// </summary>
        public virtual void PreInitialize() { }
        /// <summary>
        /// Fired one time during application start.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="e"></param>
        public virtual void Initialize(object obj, ApplicationExecutionEventArgs e) { }
        /// <summary>
        /// Fired one time after Initialize()
        /// </summary>
        public virtual void PostInitialize() { }

        protected void Application_Start()
        {
            RestBootstrapperUtility.InitializeAllRestBootstrappers(true, false, null);
        }

        protected void Application_BeginRequest()
        {
                // Because an http handler can be registered to perform routing as well we do not want the routing to be executed
                // twice.  In order to prevent this from happening the following technique is being used.
                // If this class handles the routing rewrite the URL after the routing using a unique key in the rewrite.  If the unique key
                // is found during the http module route execution the handler does not route the request for a second time and an additional
                // rewrite is done at the http handler level to remove the unique values used during the rewrite processs.
            //if (!RouteUtility.IsNativeRouteRegistrationEnabled)
            //{
            //    string redirectUri = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;

            //    if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.Url.Query))
            //    {
            //        redirectUri = "/&" + IgnoreRouteHandlerValue;
            //    }
            //    else
            //        redirectUri = "/?" + IgnoreRouteHandlerValue;

            //    RouteHandler handler = new RouteHandler();
            //    handler.ProcessRequest(System.Web.HttpContext.Current);
            //}
        }
    }
}
